//
//  CalculatorViewController.m
//  Calculator
//
//  Created by nhieu nguyen on 6/28/13.
//  Copyright (c) 2013 nhieu.nguyen. All rights reserved.
//

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController ()

@property (nonatomic) BOOL userIsInTheMiddleofEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;

@end

@implementation CalculatorViewController


@synthesize display = _display;
@synthesize userIsInTheMiddleofEnteringANumber = _userIsInTheMiddleofEnteringANumber;
@synthesize brain = _brain;


- (CalculatorBrain *)brain
{
    if (!_brain) {
        _brain = [[CalculatorBrain alloc] init];
    }
    return _brain;
}
- (IBAction)digitPressed:(UIButton *)sender {
    NSString *digit = [sender currentTitle];
    if (self.userIsInTheMiddleofEnteringANumber) {
        self.display.text = [self.display.text stringByAppendingString:digit];
    } else {
        self.userIsInTheMiddleofEnteringANumber = YES;
        self.display.text = digit ;
    }
    
}
- (IBAction)enterPressed {
  //  NSLog(@"ENterPressed");
    if ([self.brain isKindOfClass:[CalculatorBrain class]]) {
        NSLog(@"La CalculatorBrain :D");
    }
    
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleofEnteringANumber = NO;
}
- (IBAction)operationPressed:(UIButton *)sender {
    if (self.userIsInTheMiddleofEnteringANumber) {
        [self enterPressed];
    }
    double result = [self.brain performOperation:[sender currentTitle]];
    self.display.text = [NSString stringWithFormat:@"%g", result];
}

/*
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setDisplay:nil];
    [super viewDidUnload];
}
 */
@end
